﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Web.Mvc;
using Autofac;
using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;

namespace Pay.Web.Areas.Admin.Controllers {
	[RouteArea("admin")]
	[RoutePrefix("itp")]
	public class ItpController : AdminController {
		private IProfitDayService _iProfitDayService;
		private IComponentContext _iocContext;

		public ItpController(IComponentContext iocContext, IProfitDayService iProfitDayService) {
			_iocContext = iocContext;
			_iProfitDayService = iProfitDayService;
		}

		public ActionResult Index() {
			return View();
		}

		public JsonResult Gets(ProfitDayQuery query) {
			var model = _iProfitDayService.Get(query);
			return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
		}

		public JsonResult Get(long id) {
			var model = _iProfitDayService.Get(id);
			return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// 生成文件
		/// </summary>
		/// <returns>The notify.</returns>
		/// <param name="dto">Dto.</param>
		[Route("code_file")]
		public async Task<JsonResult> CodeFile(PayChannel pay_channel, DateTime create_date) {
			var iPayService = _iocContext.ResolveKeyed<IPayService>(pay_channel);
			var result = await iPayService.ItpToFile(create_date);
			if (result > 0)
				return Json(ResultToJson.Json("生成成功", result), JsonRequestBehavior.AllowGet);
			return Json(ResultToJson.Json<string>("生成失败"), JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// 对账处理
		/// </summary>
		/// <returns>The back.</returns>
		/// <param name="dto">Dto.</param>
		public JsonResult Deal(PayChannel pay_channel, long id) {
			var iPayService = _iocContext.ResolveKeyed<IPayService>(pay_channel);
			var result = iPayService.DealItp(id);
			if (result > 0)
				return Json(ResultToJson.Json("对账成功", result), JsonRequestBehavior.AllowGet);
			return Json(ResultToJson.Json<string>("对账失败"), JsonRequestBehavior.AllowGet);
		}
	}
}